<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎页</title>
    <#assign base=request.contextPath  />
    <base id="basePath" href="${base}">
    <link rel="stylesheet" href="${base}/frame/layui/css/layui.css">
    <script src="${base}/frame/layui/layui.js" charset="utf-8"></script>
    <script src="${base}/js/jquery/jquery.min.js"></script>
    <script src="${base}/js/zhang.js"></script>
</head>
<body>
<form class="layui-form" action="" style="padding-top: 20px;">
    <div class="demoTable">
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">绑定状态</label>
                <div class="layui-input-inline">
                    <select id="status" lay-verify="">
                        <option value="">请选择</option>
                        <option value="已绑定">已绑定</option>
                        <option value="未绑定">未绑定</option>
                    </select>
                </div>

            </div>

            <button class="layui-btn" onclick="search()" data-method="offset" type="button"
                    style="background-color: #0092EF; margin-left: 25px">查询
            </button>

        </div>
    </div>
</form>
<div class="demoTable" style="padding:15px;">
    <button class="layui-btn" id="btn2" data-method="offset" type="button" style="background-color: #0092EF;">生成令牌
    </button>
    <button class="layui-btn" onclick="daochu()" data-method="offset" type="button" style="background-color: #0092EF;">导出
    </button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit"
            style="background: dodgerblue">编辑
    </button>
    <button type="button" onclick="deleteArea('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal"
            lay-event="del" style="background: red">删除
    </button>
    <button type="button" onclick="prohibit('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="del"
            style="background: #0c9076">解绑
    </button>
</script>
<script>
    layui.use(['form', 'layedit', 'laydate', 'table', 'carousel'], function () {

        var carousel = layui.carousel;
        var form = layui.form
            , $ = layui.jquery
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table;


        var whereData = {}
        whereData.status = $("#status").val();

        table.render({
            elem: '#test'
            , url: '${base}/user/selectMyPage'
            , title: '用户'
            , totalRow: true
            , toolbar: '#toolbarDemo'//打印
            , cols: [[
                {
                    field: 'id', title: '序号', templet: function (val) {
                        return val.LAY_INDEX
                    }, field: true, align: 'center', width: 80
                }
                , {field: 'token', title: '令牌1', align: 'center'}
                , {field: 'token2', title: '令牌2', align: 'center',sort: true}
                , {field: 'extractionCode', title: '提取码', align: 'center'}
                , {field: 'receipt', title: '回执', align: 'center'}
                , {field: 'remarks', title: '备注', align: 'center'}
                , {field: 'status', title: '手机号是否绑定', align: 'center', width: 130}
                , {field: 'tel', title: '手机号', align: 'center'}
                , {
                    field: 'createTime', title: '创建时间', templet: function (val) {
                        if (val.createTime == undefined) {
                            return "";
                        }
                        ;
                        if (val.createTime == 0) {
                            return "";
                        }
                        return changeDateFormat(val.createTime);
                    }, align: 'center'
                }
                , {
                    field: 'updateTime', title: '修改时间', templet: function (val) {
                        if (val.updateTime == undefined) {
                            return "";
                        }
                        ;
                        if (val.updateTime == 0) {
                            return "";
                        }
                        return changeDateFormat(val.updateTime);
                    }, align: 'center'
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', align: 'center'}
            ]]
            , id: 'testReload'
            , page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                , theme: '#0092EF'
                , limit: 10
            }
            , done: function (res, curr, count) {
                this.where = {};
            }
        });
        var active = {
            offset: function (othis) {
                var type = othis.data('type')
                    , text = othis.text();
                layer.open({
                    type: 2,
                    title: '生成令牌',
                    area: ['400px', '200px'], //高 宽
                    shadeClose: false,
                    //弹出的页面路径
                    content: '${base}/user/add.html',//路径

                });
            }
        };

        $('#btn2').on('click', function () {
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });

        //编辑
        table.on('tool(test)', function (obj) {
            var layEvent = obj.event;
            if (layEvent == "edit") {
                var data = obj.data;
                //示范一个公告层
                layer.open({
                    type: 2,
                    title: '编辑',
                    shadeClose: false,
                    area: ['400px', '400px'],
                    content: '${base}/user/edit.html?id=' + data.id,
                    success: function (layero, index) {
                        var iframeWin = window[layero.find
                        ('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.$("#receipt").val(data.receipt)
                        iframeWin.$("#remarks").val(data.remarks)
                        //渲染一下form表单
                        iframeWin.layui.form.render('select')
                    }
                });
            }
        });


    });


    //删除
    function deleteArea(id) {
        layer.confirm('确定删除?', function (index) {
            var jsonDate = {};
            jsonDate.id = id;
            $.ajax({
                type: "POST", //提交方式
                contentType: "application/json;charset=UTF-8",
                url: "${base}/user/deleteById",
                data: JSON.stringify(jsonDate),
                sync: false,
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg("删除成功")
                        var table = layui.table;
                        //表刷新方法
                        table.reload('testReload', {
                            url: '${base}/user/selectMyPage'
                            , where: {} //设定异步数据接口的额外参数
                        });
                        //关闭删除是否删除的提示框
                        layer.close(index);
                    } else {
                        layer.msg(data.msg)
                    }
                }
            });
        });
    }


    function search() {
        var whereData = {}
        whereData.status = $("#status").val();
        //表刷新方法
        var table = layui.table;
        table.reload('testReload', {
            url: '${base}/user/selectMyPage'//数据接口
            , where: whereData
        });
    }

    //解绑
    function prohibit(id) {
        layer.confirm('确定解绑此手机号?', function (index) {
            var jsonDate = {};
            jsonDate.id = id;
            $.ajax({
                type: "POST", //提交方式
                url: "${base}/user/prohibit",
                data: {"id": id},
                sync: false,
                success: function (data) {
                    // alert(JSON.stringify(data))
                    if (data.code === 0) {
                        layer.msg("解绑成功")
                        var table = layui.table;
                        //表刷新方法
                        table.reload('testReload', {
                            url: '${base}/user/selectMyPage'
                            , where: {} //设定异步数据接口的额外参数
                        });
                        //关闭删除是否删除的提示框
                        layer.close(index);
                    } else {
                        layer.msg(data.msg)
                    }
                }
            });
        });
    }

    /**
     *  导出
     */
    function daochu() {
        var status;
        if ($("#status").val() == "") {
            status = "未绑定";
        } else {
            status = $("#status").val();
        }
        window.location.href = "${base}/user/createExcel?status=" + status;
    }


</script>
</body>
</html>